package com.example.demo.service;

import com.example.demo.mapper.AdminMapper;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.mapper.OlderMapper;
import com.example.demo.model.*;
import com.example.demo.tools.Constant;
import com.example.demo.tools.EncryptionTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-18
 * Time: 12:03
 */
@Service
public class HomeService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private OlderMapper olderMapper;
    @Autowired
    private EncryptionTool encryptionTool;      // 加密工具

    // 处理用户登录操作
    public Object login(HttpServletRequest request,String phone, String password, String status){
        HttpSession session = request.getSession(true);
        // 根据手机号和身份查询用户信息
        LoginUser user = adminMapper.getAdminByNameAndPassword(phone,status);
        if(user != null) {
            boolean ret = encryptionTool.decrypt(password,user.getPassword());
            if(ret){
                // 清除掉密码
                user.setPassword("");
                // 放到 session 中
                session.setAttribute(Constant.USER_INFO_SESSION_KEY,user);

                // 更新上一次登录的时间
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String currentDate = format.format(new Date());
                String oldDate = format.format(user.getLogin_time());
                Integer n = adminMapper.updateTimeById(user.getId(), currentDate, oldDate);
                if(n != 1){
                    return "更新时间失败!";
                }
                if(status.equals("超级管理员")){
                    Admin admin = adminMapper.getDetailAdminInfoById(user.getId());
                    admin.setPassword("");
                    return admin;
                }else if(status.equals("员工")){
                    Employee employee = employeeMapper.getDetailInfoByEmployeeId(user.getId());
                    employee.setPassword("");
                    return employee;
                }else{
                    Older older = olderMapper.getOlderAllInfo(user.getId());
                    older.setPassword("");
                    return older;
                }
            }
        }
        return "用户名或密码错误!";
    }

//    获取柱状图的数据
    public Object getOption() {

        Integer[] arr = new Integer[3];

        List<LoginUser> adminTotal = adminMapper.getAdminTotal();
        List<LoginUser> employeeList = employeeMapper.getEmployeeList(null);
        List<LoginUser> olderList = olderMapper.getOlderList(null);

        if(adminTotal != null && employeeList != null && olderList != null){

            arr[0] = adminTotal.size();
            arr[1] = employeeList.size();
            arr[2] = olderList.size();

            return arr;
        }
        return "柱状图数据获取失败!";
    }

//    公告数据获取
    public Object getNoticeList(Boolean isNotice) {
        List<Notice> allNotice = adminMapper.getAllNotice();
        if(allNotice != null && allNotice.size() != 0){
//            检查需不需要截取
            if(isNotice){
                Constant.interceptBefore21(allNotice);
            }
            return allNotice;
        }else{
            return "公告数据获取失败!";
        }
    }
}
